<template>
  <div class="register-container">
    <div class="register-form">
      <h2 class="title">注册账号</h2>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-position="top">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-lock"
            show-password
          />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="el-icon-message" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号" prefix-icon="el-icon-phone" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleRegister"
            class="register-button"
            round
            :disabled="loading"
          >
            注册
          </el-button>
        </el-form-item>
        <div class="links">
          <router-link to="/login">已有账号，去登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import { validateEmail, validatePhone } from '../utils/common'
import { register } from '../api/user'

export default {
  name: 'RegisterPage',
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        email: '',
        phone: ''
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' },
          { validator: this.checkUsername, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: this.validateEmail, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: this.validatePhone, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async checkUsername(rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入用户名'))
      }
      // 这里可以添加用户名唯一性检查
      callback()
    },
    validateConfirmPassword(rule, value, callback) {
      if (!value) {
        return callback(new Error('请确认密码'))
      }
      if (value !== this.registerForm.password) {
        return callback(new Error('两次输入的密码不一致'))
      }
      callback()
    },
    validateEmail(rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入邮箱'))
      }
      if (!validateEmail(value)) {
        return callback(new Error('邮箱格式不正确'))
      }
      callback()
    },
    validatePhone(rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入手机号'))
      }
      if (!validatePhone(value)) {
        return callback(new Error('手机号格式不正确'))
      }
      callback()
    },
    async handleRegister() {
      try {
        await this.$refs.registerFormRef.validate()
        this.loading = true
        
        const formData = {
          username: this.registerForm.username,
          password: this.registerForm.password,
          email: this.registerForm.email,
          phone: this.registerForm.phone
        }
        
        // 直接调用注册API
        await register(formData)
        
        ElMessage.success('注册成功，请登录')
        this.$router.push('/login')
      } catch (error) {
        ElMessage.error(error.response?.data?.message || '注册失败，请稍后重试')
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-form {
  width: 100%;
  max-width: 450px;
  background: #fff;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.title {
  text-align: center;
  color: #303133;
  margin-bottom: 30px;
  font-size: 24px;
  font-weight: 600;
}

.register-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
}

.links {
  text-align: center;
  margin-top: 20px;
}

.links a {
  color: #409eff;
  text-decoration: none;
}

.links a:hover {
  color: #66b1ff;
}

@media (max-width: 480px) {
  .register-form {
    padding: 24px;
  }
}
</style>